package com.hxzy.action;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.struts2.ServletActionContext;

import com.hxzy.entity.User;
import com.hxzy.service.UserService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport {

	private static final long serialVersionUID = 872219473624611910L;
	private String name;
	private String passwd;
	private String remeberMe;
	private UserService userService;
	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		ServletActionContext.getResponse().setCharacterEncoding("utf-8");
		ServletActionContext.getResponse().setContentType("application/json");
		User user = userService.login(name, passwd,ServletActionContext.getRequest().getRemoteAddr(),remeberMe);
		if (user!=null&&user.getPasswd().equals(DigestUtils.sha1Hex((user.getSalt() + passwd).getBytes()))) {
			Map<String,Object> session=ActionContext.getContext().getSession();
			session.put("userLogin", user);
			//ajax判断是否登录成功
			ServletActionContext.getResponse().getWriter().print("{\"status\":1,\"message\":\"登陆成功\"}");
			return null;
		}else{
			ServletActionContext.getResponse().getWriter().print("{\"status\":2,\"message\":\"登陆失败\"}");
			return null;
		}
	}


	public String getRemeberMe() {
		return remeberMe;
	}

   
    public UserService getUserService() {
        return userService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public void setRemeberMe(String remeberMe) {
		this.remeberMe = remeberMe;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPasswd() {
		return passwd;
	}

	public void setPasswd(String passwd) {
		this.passwd = passwd;
	}
}
